﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AdminSite.Models;

namespace AdminSite
{
    public partial class manageCurrency : System.Web.UI.Page
    {
        private CurrencyManagerAdminDBEntities _db = new CurrencyManagerAdminDBEntities();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //ddlCompanies.SelectedValue = ddlCompanies.Items[0].Value;
                ViewState["CompanyId"] = 1;
                
                BindDataToCurrencyGridView();
            }
        }

        /// <summary>
        /// Fill ddlCompanies DropDownList with companies list from DB
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Company> GetCompanies()
        {
            return _db.Companies;
        }

        /// <summary>
        /// Binds data to gvCurrency GridView due to selected company
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BindDataToCurrencyGrid(object sender, EventArgs e)
        {
            ViewState["CompanyId"] = ((DropDownList)sender).SelectedValue;
            gvCurrency.SelectedIndex = -1;
            pnlCurrencyDetails.Visible = false;
            BindDataToCurrencyGridView();
        }


        

        /// <summary>
        /// Fills panel for editing current currency
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void EditCurrent(object sender, EventArgs e)
        {
            ViewState["CurrentCurrencyId"] = ((GridView)sender).SelectedDataKey.Value;
            Currency currentCurrency = GetCurrencyById(GetCurrencyId());
            ViewState["OriginalBuyValue"] = currentCurrency.BankBuy;
            ViewState["OriginalSellValue"] = currentCurrency.BankSell;
            pnlCurrencyDetails.Visible = true;
            BuyValue.Text = currentCurrency.BankBuy.ToString();
            SellValue.Text = currentCurrency.BankSell.ToString();
            //int currentCurrencyId = GetCurrencyId();
            //IEnumerable<Currency> currentCurrency = _db.Currencies.Where(c => c.CurrencyId == currentCurrencyId);


        }

        /// <summary>
        /// Gets the entity of Currency class
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private Currency GetCurrencyById(int id)
        {
            return _db.Currencies.SingleOrDefault(c => c.CurrencyId == id);
        }

        /// <summary>
        /// Validates the accuracy of currency values input
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>

        protected void ValidateCurrencyBuyValues(object source, ServerValidateEventArgs args)
        {
            decimal _buyValue;
            decimal.TryParse(BuyValue.Text, out _buyValue);
            if (_buyValue <= 0)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void ValidateCurrencySellValues(object source, ServerValidateEventArgs args)
        {
            decimal _sellValue;
            decimal.TryParse(SellValue.Text, out _sellValue);
            if (_sellValue <= 0)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        /// <summary>
        /// Uplade values for current currency
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UpdateCurrency(object sender, CommandEventArgs e)
        {
            Currency currentCurrency = GetCurrencyById(GetCurrencyId());
            currentCurrency.BankBuy = Convert.ToDecimal(BuyValue.Text);
            currentCurrency.BankSell = Convert.ToDecimal(SellValue.Text);
            _db.SaveChanges();
            BindDataToCurrencyGridView();
            gvCurrency.SelectedIndex = -1;
            pnlCurrencyDetails.Visible = false;

        }

        /// <summary>
        /// Refactoring for binding operations steps
        /// </summary>
        private void BindDataToCurrencyGridView()
        {
            int companyId = GetCompanyId();
            IEnumerable<Currency> currency = _db.Currencies.Where(c => c.CompanyId == companyId);
            gvCurrency.DataSource = currency.ToList();
            gvCurrency.DataBind();
           
        }

        /// <summary>
        /// Cancels updating action
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CancelUpdating(object sender, CommandEventArgs e)
        {
            gvCurrency.SelectedIndex = -1;
            pnlCurrencyDetails.Visible = false;
        }

        /// <summary>
        /// Method to parse value of current company id from ViewState
        /// </summary>
        /// <returns></returns>
        private int GetCompanyId()
        {
            if (ViewState["CompanyId"] == null)
            {
                return 1;
            }
            else
            {
                return int.Parse(ViewState["CompanyId"].ToString());
            }
        }


        /// <summary>
        /// Method to parse value of current currency id from ViewState
        /// </summary>
        /// <returns></returns>
        private int GetCurrencyId()
        {

            return int.Parse(ViewState["CurrentCurrencyId"].ToString());

        }
    }
}